<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="fromList" :cols="1" labelWidth="130" :isSubmitBtn="false"></FormPanel>
    </div>

    <div :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
      <AjaxButton type="primary" :disabled="newStatus" :click="saveDialog">保 存</AjaxButton>
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
    <BaseDialog :visible.sync="mainDamagePart" :title="'备件查询'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <SelectMainDamagePart @close="mainDamagePartClose" @save="mainDamagePartSave"></SelectMainDamagePart>
    </BaseDialog>
  </div>
</template>

<script>
import { queryModel, addModel } from '@/api/repair/repairBusiness/model/index.js';
import { getBrand, getSeries, getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getConfigHeaders } from '@/api/fetch';
import { jsonToHump } from '@/utils/index';
import { dictionary } from '@/utils/dictMixin';
import { addPart, editPart, getListByPage } from '@/api/claim/dealer/AuthMaintenance';
import SelectMainDamagePart from './vehicle.vue';
export default {
  props: ['row', 'type','currentUserInfo'],
  mixins: [dictionary],
  components: {
    SelectMainDamagePart
  },
  data() {
    return {
      fromList: this.createFromList(),
      modelId: undefined,
      isShowGdcx: false,
      fileList: [],
      getConfigHeaders: getConfigHeaders,
      newStatus: false,
      formLabelPro: {},
      mainDamagePart: false,
      rowDate: {},
      levelData: []
    };
  },
  created() {
    // this.fromList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
  },
  computed: {
  },
  mounted() {
    if (this.type == 'edit') {
      this.fromList.forEach(x => {
        x.initialValue = this.row[x.fieldName];
      });;
    }
  },
  methods: {
    //获取预授权级别
    getLevelData() {
      let arr = []
      getListByPage().then(res => {
        if (res.data) {
          res.data.forEach((item, i) => {
            arr.push({ text: `${item.name}-${this.createDictText(item.authType,'5508')}`, value: item.id })
          })
          this.levelData = arr;
        }
      })
      return arr
    },
    //备件查询
    mainDamagePartClose() {
      this.mainDamagePart = false;
    },
    async mainDamagePartSave(obj) {
      this.fromList.map(x => {
        if ('partCode' == x.fieldName) {
          x.initialValue = obj[0].partNo;
        }
        if ('partName' == x.fieldName) {
          x.initialValue = obj[0].partName;
        }
      });
      this.rowDate.partCode = obj[0].partNo;
      this.mainDamagePart = false;
    },
    createFromList() {
      return [
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'partCode',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.selectParts()}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '备件名称',
          fieldName: 'partName',
          disabled: true,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '预授权级别',
          fieldName: 'name',
          itemList: this.getLevelData(),
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      ];
    },
    //选择备件
    selectParts() {
      this.mainDamagePart = true
    },
    async saveDialog() {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();// 获取from改变时整个表单的值
      if (err) return;
      let json = JSON.parse(JSON.stringify(data))
      // jsonToHump(json);
      let params = Object.assign({},this.row,json);
      params.partName = json.partName
      params.partCode = json.partCode
      params.createdBy = this.currentUserInfo.createdById;
      this.levelData.forEach((item) => {
        if (item.text === json.name || item.value === json.name) {
          params.lId = item.value;
          params.name = item.text;
        }
      });

      if (this.type === 'add') {
        //新增
        let res = await addPart(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '保存成功!!' });
          // this.fromList.forEach(x => (x.disabled = true));
          this.newStatus = true;
          this.$emit('close');
        }
      } else if (this.type === 'edit') {
        //编辑
        params.ptId = this.row.ptId;
        params.updatedBy = this.currentUserInfo.updatedById;
        let res = await editPart(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '修改成功!!' });
          // this.fromList.forEach(x => (x.disabled = true));
          this.newStatus = true;
          this.$emit('close');
        }
      }

    },
    closeDialog() {
      this.$emit('close');
    },
  }
};
</script>
